import React, { useState, useEffect } from 'react'
import { Server, Users, Route as RouteIcon, Activity, Cpu } from 'lucide-react'

function Dashboard() {
  const [stats, setStats] = useState({
    totalRequests: 0,
    activeConnections: 0,
    registeredUsers: 0,
    routeRules: 0
  })

  useEffect(() => {
    // 模拟数据加载
    const loadStats = async () => {
      // 这里应该调用后端API获取实时数据
      setStats({
        totalRequests: 12458,
        activeConnections: 342,
        registeredUsers: 156,
        routeRules: 28
      })
    }

    loadStats()
    const interval = setInterval(loadStats, 5000)
    return () => clearInterval(interval)
  }, [])

  const statCards = [
    {
      title: '总请求数',
      value: stats.totalRequests.toLocaleString(),
      icon: Activity,
      color: 'bg-gradient-to-r from-blue-500 to-cyan-500',
      change: '+12.5%'
    },
    {
      title: '活跃连接',
      value: stats.activeConnections,
      icon: Cpu,
      color: 'bg-gradient-to-r from-green-500 to-emerald-500',
      change: '+3.2%'
    },
    {
      title: '注册用户',
      value: stats.registeredUsers,
      icon: Users,
      color: 'bg-gradient-to-r from-purple-500 to-pink-500',
      change: '+5.8%'
    },
    {
      title: '路由规则',
      value: stats.routeRules,
      icon: RouteIcon,
      color: 'bg-gradient-to-r from-orange-500 to-red-500',
      change: '+2.1%'
    }
  ]

  return (
    <div className="space-y-6">
      {/* Header */}
      <div className="bg-white/10 backdrop-blur-lg rounded-2xl p-6 border border-white/20">
        <h1 className="text-3xl font-bold text-white mb-2">系统概览</h1>
        <p className="text-blue-200">实时监控网关系统的运行状态和性能指标</p>
      </div>

      {/* Stats Grid */}
      <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6">
        {statCards.map((card) => {
          const Icon = card.icon
          return (
            <div key={card.title} className="bg-white/10 backdrop-blur-lg rounded-2xl p-6 border border-white/20 hover:scale-105 transition-transform duration-200">
              <div className="flex items-center justify-between">
                <div>
                  <p className="text-blue-200 text-sm font-medium">{card.title}</p>
                  <p className="text-3xl font-bold text-white mt-2">{card.value}</p>
                  <p className="text-green-400 text-sm mt-1">{card.change}</p>
                </div>
                <div className={`p-3 rounded-full ${card.color}`}>
                  <Icon className="h-6 w-6 text-white" />
                </div>
              </div>
            </div>
          )
        })}
      </div>

      {/* Quick Actions */}
      <div className="bg-white/10 backdrop-blur-lg rounded-2xl p-6 border border-white/20">
        <h2 className="text-xl font-bold text-white mb-4">快速操作</h2>
        <div className="grid grid-cols-1 md:grid-cols-3 gap-4">
          <button 
            onClick={() => window.location.href = '/routes'}
            className="bg-blue-600 hover:bg-blue-700 text-white py-3 px-4 rounded-lg font-medium transition-colors duration-200"
          >
            添加路由规则
          </button>
          <button 
            onClick={() => window.location.href = '/users'}
            className="bg-green-600 hover:bg-green-700 text-white py-3 px-4 rounded-lg font-medium transition-colors duration-200"
          >
            创建新用户
          </button>
          <button 
            onClick={() => window.location.href = '/gateway'}
            className="bg-purple-600 hover:bg-purple-700 text-white py-3 px-4 rounded-lg font-medium transition-colors duration-200"
          >
            系统设置
          </button>
        </div>
      </div>
    </div>
  )
}

export default Dashboard